package com.example.demo.service.process;

import com.example.demo.commom.CourseVoSwap;
import com.example.demo.dto.process.AddCourseDto;
import com.example.demo.dto.process.UpdateAttendanceDto;
import com.example.demo.entity.process.Attendance;
import com.example.demo.entity.process.Course;
import com.example.demo.entity.process.CourseCode;
import com.example.demo.entity.user.ClassInfo;
import com.example.demo.entity.user.Student;
import com.example.demo.mapper.process.AttendanceMapper;
import com.example.demo.mapper.process.CourseCodeMapper;
import com.example.demo.mapper.process.CourseMapper;
import com.example.demo.mapper.user.ClassMapper;
import com.example.demo.mapper.user.StudentMapper;
import com.example.demo.vo.process.AttendanceVo;
import com.example.demo.vo.process.CourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:龙宝
 * Date:2023-03-31
 * Time:18:37
 */
@Service
public class AttendanceService {
    @Autowired
    private AttendanceMapper attendanceMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ClassMapper classMapper;

    // 修改考勤
    public Integer add(UpdateAttendanceDto updateAttendanceDto){
        // 查看考勤是否已经存在了
        if(updateAttendanceDto.getAttendance_id() == null
                || attendanceMapper.getAttendanceById(updateAttendanceDto.getAttendance_id()) == null){
            // 新增考勤
            return attendanceMapper.addAttendance(updateAttendanceDto);
        }
        return attendanceMapper.updateAttendance(updateAttendanceDto.getAttendance_id(), updateAttendanceDto.getCount());
    }



    // 查询所有考勤
    public List<AttendanceVo> getAttendanceAll(){
        List<Attendance> res = attendanceMapper.getAttendanceAll();
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            vo.setUsername(student.getUsername());
            // 课程详情
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            vos.add(vo);
        }
        return vos;
    }

    // 查看指定班级考勤信息
    public List<AttendanceVo> getAttendanceByClassId(Integer class_id){
        // 简单粗暴，查所有，再筛选
        List<Attendance> res = attendanceMapper.getAttendanceAll();
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            if(!student.getClass_id().equals(class_id)){
                continue;
            }
            vo.setUsername(student.getUsername());
            // 课程详情
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            // 其他
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());

            vos.add(vo);
        }
        return vos;
    }

    // 查看指定班级的指定学期的考勤信息
    public List<AttendanceVo> getAttendanceByClassIdAndSemester(Integer class_id, Integer semester){
        List<Attendance> res = attendanceMapper.getAttendanceAll();
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            if(!student.getClass_id().equals(class_id)){
                continue;
            }
            vo.setUsername(student.getUsername());
            // 课程详情
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            if(!course.getSemester().equals(semester)){
                continue;
            }
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            // 其他
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());

            vos.add(vo);
        }
        return vos;
    }
    // 查看指定班级的指定课程的考勤信息
    public List<AttendanceVo> getAttendanceByClassIdAndCourseId(Integer class_id, Integer course_id){
        List<Attendance> res = attendanceMapper.getAttendanceAll();
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            if(!student.getClass_id().equals(class_id)){
                continue;
            }
            vo.setUsername(student.getUsername());
            // 课程详情
            if(!attendance.getCourse_id().equals(course_id)){
                continue;
            }
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            // 其他
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());

            vos.add(vo);
        }
        return vos;
    }
    // 查看指定课程的考勤信息
    public List<AttendanceVo> getAttendanceByCourseId(Integer course_id){
        List<Attendance> res = attendanceMapper.getAttendanceAll();
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            vo.setUsername(student.getUsername());
            // 课程详情
            if(!attendance.getCourse_id().equals(course_id)){
                continue;
            }
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            // 其他
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());

            vos.add(vo);
        }
        return vos;
    }
    // 查看指定学生的考勤信息
    public List<AttendanceVo> getAttendanceByStudentId(Integer student_id){
        List<Attendance> res = attendanceMapper.getAttendanceByStudentId(student_id);
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            vo.setUsername(student.getUsername());
            // 课程详情
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            vos.add(vo);
        }
        return vos;
    }
    // 查看指定学生的指定学期的考勤信息
    public List<AttendanceVo> getAttendanceByStudentIdAndSemester(Integer student_id, Integer semester){
        List<Attendance> res = attendanceMapper.getAttendanceByStudentId(student_id);
        List<AttendanceVo> vos = new ArrayList<>();
        for(int i = 0;i<res.size();i++){
            Attendance attendance = res.get(0);
            AttendanceVo vo = new AttendanceVo();
            vo.setAttendance_id(attendance.getAttendance_id());
            vo.setCourse_id(attendance.getCourse_id());
            vo.setStudent_id(attendance.getStudent_id());
            vo.setCount(attendance.getCount());
            // 姓名
            Student student = studentMapper.getStudentById(attendance.getStudent_id());
            vo.setUsername(student.getUsername());
            // 课程详情
            Course course = courseMapper.getCourseById(attendance.getCourse_id());
            if(!course.getSemester().equals(semester)){
                continue;
            }
            vo.setCourse_name(course.getCourse_name());
            // 班级详情
            ClassInfo classInfo = classMapper.getClassInfoByClassId(student.getClass_id());
            vo.setClassInfo(classInfo);
            vos.add(vo);
        }
        return vos;
    }
}
